package com.artron.mediaartron.ui.fragment.center;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.artron.baselib.base.BaseStaticFragment;
import com.artron.baselib.entity.Response;
import com.artron.baselib.utils.ToastUtil;
import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.net.RetrofitHelper;
import com.artron.mediaartron.data.net.api.LoginApi;
import com.artron.mediaartron.util.RegexUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.functions.Action1;

/**
 * Created by qi on 2017/12/11.
 */

public class ForgotPasswordFragment extends BaseStaticFragment {

    @BindView(R.id.fragment_forgot_password_et_phone)
    protected EditText mEtPhone;
    @BindView(R.id.fragment_forgot_password_etCode)
    protected EditText mEtCode;
    @BindView(R.id.fragment_forgot_password_tv_sendCode)
    protected TextView mTvSendCode;
    @BindView(R.id.fragment_forgot_password_et_new_pass)
    protected EditText mEtNewPass;
    @BindView(R.id.fragment_forgot_password_iv_passIsVisible)
    protected ImageView mIvPassIsVisible;
    @BindView(R.id.fragment_forgot_password_btn_ensure)
    protected Button mBtnEnsure;
    private boolean isShowPassword = true;

    public static ForgotPasswordFragment newInstance() {
        ForgotPasswordFragment fragment = new ForgotPasswordFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragmrnt_forgot_password;
    }

    @Override
    protected void initView() {

    }


    @OnClick({R.id.fragment_forgot_password_tv_sendCode, R.id.fragment_forgot_password_iv_passIsVisible, R.id.fragment_forgot_password_btn_ensure})
    public void onViewClicked(View view) {
        String phoneNumber = mEtPhone.getText().toString().trim();
        String code = mEtCode.getText().toString().trim();
        String newPass = mEtNewPass.getText().toString().trim();
        switch (view.getId()) {
            case R.id.fragment_forgot_password_tv_sendCode://发送验证码
                sendVerificationCode(phoneNumber);
                break;
            case R.id.fragment_forgot_password_iv_passIsVisible://密码是否可见
                if (isShowPassword) {
                    mEtNewPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mIvPassIsVisible.setImageResource(R.drawable.ic_password_visible);
                } else {
                    mEtNewPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mIvPassIsVisible.setImageResource(R.drawable.ic_password_not_visible);
                }
                isShowPassword = !isShowPassword;
                break;
            case R.id.fragment_forgot_password_btn_ensure://重置完成
                resetPassword(phoneNumber, code, newPass);
                break;
        }
    }

    private void sendVerificationCode(final String number) {
        if (!TextUtils.isEmpty(number) && RegexUtils.isTelPhoneNum(number)) {
            LoginApi loginApi = RetrofitHelper.getLoginApi();
            RetrofitHelper.subscript(loginApi.getCaptcha(number), new Action1<Response>() {
                @Override
                public void call(Response response) {
//                    if ("OK".equals(response.getStatus())) {
//                        Altar altar = new Altar();
//                        User user = altar.getUser();
//                        user.setMobile(number);
//                    }
                    ToastUtil.show(response.getMessage());
                }
            });
        } else {
            ToastUtil.show("请输入正确的手机号");
        }
    }

    private void resetPassword(String mobile, String captcha, String password) {

        if (isNull(mobile, "手机号码不能为空") && isNull(captcha, "验证码不能为空") && isNull(password, "密码不能为空") && RegexUtils.isTelPhoneNum(mobile)) {

            LoginApi loginApi = RetrofitHelper.getLoginApi();
            RetrofitHelper.subscript(loginApi.resetPassword(mobile, captcha, password),
                    new Action1<Response>() {
                        @Override
                        public void call(Response response) {
                            View view = getView();
                            if (view != null) {
                                UIUtils.showSnackbar(view, "重置成功");
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        getActivity().onBackPressed();
                                    }
                                }, 1000);
                            }
                        }
                    });

        }
    }

    private boolean isNull(String s, String masg) {

        if (TextUtils.isEmpty(s)) {
            ToastUtil.show(masg);
            return false;
        }
        return true;
    }
}
